/**
 * Project Name:easyJava
 * File Name:Test.java
 * Package Name:com.ulewo.easyjava
 * Date:2016年7月30日下午7:02:58
 * Copyright (c) 2016, ulewo.com All Rights Reserved.
 */

package com.ulewo.easyjava;

import com.ulewo.easyjava.bean.ColumnInfo;
import com.ulewo.easyjava.bean.DataTableInfo;
import com.ulewo.easyjava.builder.*;
import com.ulewo.easyjava.framwork.BuildFramework;
import com.ulewo.easyjava.utils.Constants;
import com.ulewo.easyjava.utils.PropertiesUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ClassName:Test <br/>
 * Date:     2016年7月30日 下午7:02:58 <br/>
 * @author 多多洛
 *         Copyright (c) 2016, ulewo.com All Rights Reserved.
 */
public class MainApp {

    private static Connection conn = null;

    static {
        String driverName = PropertiesUtils.getString("db.driver.name");
        String url = PropertiesUtils.getString("db.url");
        String user = PropertiesUtils.getString("db.username");
        String password = PropertiesUtils.getString("db.password");
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<DataTableInfo> tableList = ReadTable.readTable();
        for (DataTableInfo table : tableList) {
            BuildFramework.initFramwork(table);
            //创建bean
            BuildBean.buildEntityBean(table);
            //创建参数bean
            BuildParam.buildEntityParam(table);
            //创建mapper
            BuildMapper.buildMapper(table);
            //创建XML
            BuildMapperXml.buildMapperXml(table);
            //创建service
            BuildService.buildService(table);
            //创建serviceimpl
            BuildServiceImpl.buildServiceImpl(table);
            //创建controller
            BuildController.buildController(table);
        }

        //ALTER TABLE `student` COMMENT '学生表2.0';
        // ALTER TABLE `student` MODIFY COLUMN `id` COMMENT '学号';
/*
        PreparedStatement pstate = null;
        ResultSet results = null;
        ResultSet clumnResult = null;
        try {
            pstate = conn.prepareStatement("show table status");
            results = pstate.executeQuery();
            //获取表信息
            while (results.next()) {
                String tableName = results.getString("NAME");
                String comment = results.getString("COMMENT");
                System.out.println("#更新表" + tableName + "备注");
                if (null != comment && !"".equals(comment)) {
                    System.out.println("ALTER TABLE " + tableName + " COMMENT '" + comment + "';");
                }


                //获取字段信息
                pstate = conn.prepareStatement("show full fields from " + tableName);
                clumnResult = pstate.executeQuery();
                while (clumnResult.next()) {
                    String fieldName = clumnResult.getString("FIELD");
                    String fieldComment = clumnResult.getString("COMMENT");
                    String fieldType = clumnResult.getString("TYPE");
                    String isNull = clumnResult.getString("NULL");
                    String defaultValue = clumnResult.getString("DEFAULT");
                    String extra = clumnResult.getString("EXTRA");
                    StringBuffer sb = new StringBuffer("ALTER TABLE ").append(tableName).append(" MODIFY COLUMN ").append(fieldName).append(" ").append(fieldType);

                    if (null != defaultValue) {
                        sb.append(" ").append("DEFAULT '" + defaultValue + "'");
                    } else {
                        sb.append(" ").append("DEFAULT NULL");
                    }
                    if ("auto_increment".equalsIgnoreCase(extra)) {
                        sb.append(" ").append(" AUTO_INCREMENT");
                    }
                    sb.append(" COMMENT ").append("'" + fieldComment + "'");
                    System.out.println(sb + ";");

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != clumnResult) {
                try {
                    clumnResult.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (null != results) {
                try {
                    results.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }*/
    }

}
